package com.lankystar.sos.action;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.lankystar.sos.service.IUserInterface;
import com.lankystar.util.ResUtil;
import com.opensymphony.xwork2.ActionSupport;

@ParentPackage("sos")
@Namespace("/")
@Component
@Scope("prototype")
@Results({
	@Result(name="success",type="json")
})
public class FileAction extends ActionSupport{
	/**
	 * 图片上传
	 * @author SYX
	 *
	 */

	private static final long serialVersionUID = 1L;
	@Autowired
	private IUserInterface userInterface;
	private File idCardFront;
	private File idCardBack;
	private File userPortrait;
	private Integer userId;
	private Integer userkey;
	private String idCard;
	//提交过来的file 的MIME类型
	private String fileContentType;
	
	public Integer getUserId() {
		return userId;
	}
	public void setUserId(Integer userId) {
		this.userId = userId;
	}
	public String getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}
	public String getIdCard() {
		return idCard;
	}
	public void setIdCard(String idCard) {
		this.idCard = idCard;
	}
	public File getIdCardFront() {
		return idCardFront;
	}
	public void setIdCardFront(File idCardFront) {
		this.idCardFront = idCardFront;
	}
	public File getIdCardBack() {
		return idCardBack;
	}
	public void setIdCardBack(File idCardBack) {
		this.idCardBack = idCardBack;
	}
	public File getUserPortrait() {
		return userPortrait;
	}
	public void setUserPortrait(File userPortrait) {
		this.userPortrait = userPortrait;
	}
	public Integer getUserkey() {
		return userkey;
	}
	public void setUserkey(Integer userkey) {
		this.userkey = userkey;
	}
	/**
	 * 上传头像
	 * @return
	 * @throws Exception
	 */
	@Action(value="upLoad")
	public String upLoadMethod() throws Exception{
		Map<String, Object> map = new HashMap<>();
		try {
			map.put("data", userInterface.userUpLoadMethod(userId, userPortrait));
			map.put("code", 1);
			map.put("msg", "上传成功！！");
		} catch (Exception e) {
			map.put("code", 0);
			map.put("msg", "上传失败！!");
		}
		ResUtil.toJson(ServletActionContext.getResponse(), map);
		return SUCCESS;
	}
	/**
	 * 用户验证方法
	 */
	@Action(value="userApprove")
	public String userApproveMethod(){
		Map<String, Object> map = new HashMap<>();
		try {
			map.put("data", userInterface.userApproveMethod(idCardBack, idCardFront, idCard, userId, userkey));
			map.put("code", 1);
			map.put("msg", "正在验证中！！");
		} catch (Exception e) {
			map.put("code", 0);
			map.put("msg", "文件上传失败！！");
		}
		ResUtil.toJson(ServletActionContext.getResponse(), map);
		return SUCCESS;
	}

}
